import {
    getInsuranceRecharge,
    getIongInsuranceRechargeDetail
} from '../../api/insurance'
import { getPayRechargeRecord } from '../../../api/app'
import {
    dateFormat,
    toast
} from '../../../utils/util.js'
import user from '../../../utils/user'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        data: [],
        insureCompanyType:'',
        productType: '',
        isHasNextPage: true,
        page:1
    },
    async onLoad (options) {
        console.log(options,12132425435);
        this.setData({
            productType:options.type
        })
        wx.setNavigationBarTitle({
            title: options.title
        })
        await user.waitLogin()
        // type=InsureRechargePeriodZgrs
        this.setData({
            insureCompanyType:user.insureCompanyType
        })
        if(this.data.productType === 'InsureRechargePeriodZgrs'){
            // 这个是长期工接口
            this.getIongInsuranceRechargeDetail()
        }else if(this.data.productType === 'payRenew'){
            // 充值接口
            console.log('这个是充值')
            this.getPayRechargeRecord()
        }else{
            this.onGetInsuranceRecharge()
        }
    },
    /**
     * 生命周期函数--监听页面显示
     */
    async onShow () {},
    onGetInsuranceRecharge(){
        // 日日保接口
        getInsuranceRecharge({
            insureCompanyType:user.insureCompanyType,
            pageNum:this.data.page,
            pageSize:10
        }).then(res => {
            if(!res.hasNextPage){
                this.setData({
                    isHasNextPage:false
                })
            }
            const arr = []
            arr.push(...this.data.data)
            arr.push(...res.list.map(item => {
                item.payMoney = item.rechargeAmount
                return item
            }))
            this.setData({
                data: arr
            })
            console.log(this.data.data, 'data')
        })
    },
    rechargeGtg() {
        // 携带参数证明页面发生改变 type：payRenew 就是续费
        wx.navigateTo({
            url: '/packageInsurance/pages/insurance-recharge-gtg/insurance-recharge-gtg?type=' + this.data.productType,
        })
    },
    // 获取长期工充值列表
    async getIongInsuranceRechargeDetail(){
        const result = await getIongInsuranceRechargeDetail({
            pageNum:this.data.page,
            pageSize:10
        })
        if(!result.hasNextPage){
            this.setData({
                isHasNextPage:false
            })
        }
        const arr = []
        arr.push(...this.data.data)
        arr.push(...result.list.map((item)=>{
            item.payMoney =  item.rechargeAmount
            return item
        }))
        this.setData({
            data: arr
        })
    },
    rechargeWechat(){
        // 微保暂不可用
        if(user.insureCompanyType === 'insure001_wesure'){
            toast('功能维护中')
            return
        }
        if(this.data.productType === 'payRenew'){
            toast('功能维护中')
            return
        }
        wx.navigateTo({
            url: '/packageInsurance/pages/insurance-recharge-wechat/insurance-recharge-wechat?type=' + this.data.productType,
        })
    },
    goNext(event) {
        console.log(event.currentTarget.dataset.status, 'event.currentTarget.dataset.status')
        if(event.currentTarget.dataset.status == 4 || event.currentTarget.dataset.status == 5){
            return
        }
        wx.navigateTo({
            url: '/packageInsurance/pages/insurance-gtg-success/insurance-gtg-success?id=' + event.currentTarget.dataset.id + '&type=' + this.data.productType,
        })
    },
    async getPayRechargeRecord () {
        const result = await getPayRechargeRecord({
            pageNum:this.data.page,
            pageSize:10
        })
        console.log(result, 'result')
        if(!result.hasNextPage){
            this.setData({
                isHasNextPage:false
            })
        }
        const arr = []
        arr.push(...this.data.data)
        arr.push(...result.list)
        this.setData({
            data:arr
        })
    },
    // 到达底部触发
    onReachBottom() {
        if(!this.data.isHasNextPage){
            return
        }
        this.setData({
            page: this.data.page += 1
        })
        if(this.data.productType === 'InsureRechargePeriodZgrs'){
            // 这个是长期工接口
            this.getIongInsuranceRechargeDetail()
        }else if(this.data.productType === 'payRenew'){
            // 充值接口
            this.getPayRechargeRecord()
        }else{
            this.onGetInsuranceRecharge()
        }
    }
})